var demo;
var property = {
    width: 1200,
    height: 600,
    toolBtns: ["start round", "end round", "task round", "node", "chat", "state", "plug", "join", "fork", "complex mix"],
    haveHead: true,
    headBtns: ["new", "open", "save", "undo", "redo", "reload"],//如果haveHead=true，则定义HEAD区的按钮
    headBtns: ["save", "undo", "redo", "reload"],//如果haveHead=true，则定义HEAD区的按钮
    haveTool: true,
    haveGroup: true,
    useOperStack: true
};
var remark = {
    cursor: "选择指针",
    direct: "结点连线",
    start: "入口结点",
    end: "结束结点",
    task: "任务结点",
    node: "自动结点",
    chat: "决策结点",
    state: "状态结点",
    plug: "附加插件",
    fork: "分支结点",
    join: "联合结点",
    // "complex mix": "复合结点",
    group: "组织划分框编辑开关"
};
var json;
$(document).ready(function () {
    var paramStr = window.location.href.split("?")[1];
    if (paramStr) {
        paramStr = decodeURI(paramStr.split("=")[1]);
        json = JSON.parse(decodeURI(paramStr))
    }
    initFlow(json);
    demo.loadData(json);
});

function initFlow() {
    demo = $.createGooFlow($("#demo"), property);
    demo.setNodeRemarks(remark);
    demo.onItemDel = function (id, type) {
        if (confirm("确定要删除该单元吗?")) {
            this.blurItem();
            return true;
        } else {
            return false;
        }
    }
    demo.onItemFocus = function (id, model) {
        var obj;
        $("#model").val(model);
        $("#id").val(id);
        if (model == "line") {
            obj = this.$lineData[id];
            $("#type").val(obj.M);
            $("#left").val("");
            $("#top").val("");
            $("#width").val("");
            $("#height").val("");
            $("#from").val(obj.from);
            $("#to").val(obj.to);
        } else if (model == "node") {
            obj = this.$nodeData[id];
            $("#type").val(obj.type);
            $("#left").val(obj.left);
            $("#top").val(obj.top);
            $("#width").val(obj.width);
            $("#height").val(obj.height);
            $("#from").val(obj.form);
            $("#to").val("");
            $(`input[name='approveRole']`).attr("checked", false);
            $(`input[name='approveRole'][value=${obj.approveRole}]`).attr("checked", true);
            form.render('radio')
            wrapCascader(obj.approveRole, obj.value)
        }
        $("#name").val(obj.value);
        return true;
    };
    demo.onItemBlur = function (id, model) {
        document.getElementById("propertyForm").reset();
        return true;
    };
}

function Export() {
    var data = JSON.stringify(demo.exportData());
    console.log(data);
    var param = {data: data, id: json.id};
    $.post('/office/flowWorkTemplate/updateFlowWorkTemplateSteps', param, function (res) {
        if (res.code == 200) {
            showAlert(res.msg, 1, function () {
                parent.layer.closeAll()
                parent.location.reload();
            })
        }
    })
}

function updateInfo(itemId) {
    let obj;
    let item = getItem();
    if (!itemId) {
        return false;
    } else if (itemId.indexOf('node') !== -1) {
        $("#" + itemId).find("td").eq(1).text(item.name);
        $("#" + itemId).find(".span").text(item.name);
        obj = demo.$nodeData[itemId];
        obj.value = item.value
    } else if (itemId.indexOf('line') !== -1) {
        $("#" + itemId).find("text").text(item.name);
        obj = demo.$lineData[itemId];
    }
    // 自定义信息
    obj.name = item.name;
    obj.approveRole = item.approveRole
}

/***获取节点详情*/
function getItem() {
    let item = {};
    $(".form_content").find("input").each(function (i, obj) {
        item[obj.id] = obj.value;
    })
    item.value = $("#demoCascader9").val();
    item.approveRole = $("[name='approveRole']:checked").val();
    var option = getValue(item.approveRole, item.value);
    item.name = option.name;
    return item;
}

function getValue(approveRole, value) {
    var result;
    switch (parseInt(approveRole)) {
        case 1:
            result = userList.find(item => item.value == value)
            break;
        case 2:
            result = roleList.find(item => item.value == value)
            result.name = result.label;
            break;
        case 3:
            result = orgList.find(item => item.value == value)
            result.name = result.label;
            break;
    }
    return result;
}

function wrapCascader(approveRole, value) {
    var data;
    $("#demoCascader9").val(value)
    switch (parseInt(approveRole)) {
        case 1:
            data = userList
            break;
        case 3:
            data = orgList
            break;
        case 2:
            data = roleList
            break;
    }
    cascader.render({
        elem: '#demoCascader9',
        data: data,
        filterable: true,
        multiple: true,
        initValue: [value],
        onChange: function (values, data) {
            $("#demoCascader9").val(data.value)
        }
    });
}
